package cn.com.elite;

import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.elite.algorithm.GUID;
import com.elitecrm.saas.service.EliteSAASException;

public class LoadPXInfoService {
	private static final Log log = LogFactory.getLog(LoadPXInfoService.class);
	
	public String loadPXInfoService(DataSource dataSource,String ep_id,Document doc,String HandleGroup_ID,String Staff_ID)throws Exception{
		String ReturnXml="";
		Element CoursewareIDEle=(Element)doc.selectSingleNode("/Root/CoursewareID");
		if(CoursewareIDEle!=null){
			String CoursewareID=CoursewareIDEle.getText();
			ReturnXml=loadPXInfoServiceImpl(dataSource,ep_id,CoursewareID,HandleGroup_ID,Staff_ID);
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamMainIDEle==null");
		}
		return ReturnXml;
	}
	public void PXHis(DataSource dataSource,String ep_id,String Staff_ID,String CoursewareID,String LearnHistoryMainID,String ListID) throws Exception{
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String BeginTime=df.format(new Date());
		String W_LearnHistoryList="W_LearnHistoryList_"+ep_id;
		String W_LearnHistoryMain="W_LearnHistoryMain_"+ep_id;
		String sql1="insert into "+W_LearnHistoryMain+"(LearnHistoryMainID,LearnStaff_ID,LearnBeginTime,CoursewareID,ListID)Values(?,?,?,?,?)";
		int[] types1=new int[]{Types.VARCHAR,Types.VARCHAR,Types.TIMESTAMP,Types.VARCHAR,Types.VARCHAR};
		Object[] params1=new Object[]{LearnHistoryMainID,Staff_ID,BeginTime,CoursewareID,ListID};
		String sql2="insert into "+W_LearnHistoryList+"(LearnHistoryMainID,ListID,LearnBeginTime)Values(?,?,?)";
		int[] types2=new int[]{Types.VARCHAR,Types.VARCHAR,Types.TIMESTAMP};
		Object[] params2=new Object[]{LearnHistoryMainID,ListID,BeginTime};
		util.UpdatePersonUseExecute(sql1, types1, params1, dataSource);
		util.UpdatePersonUseExecute(sql2, types2, params2, dataSource);
	}
	public String loadPXInfoServiceImpl(DataSource dataSource,String ep_id,String CoursewareID,String HandleGroup_ID,String Staff_ID)throws Exception{
		GUID guid=new GUID();
		GUID guid1=new GUID();
		String LearnHistoryMainID=guid.toString();
		String ListID=guid1.toString();
		PXHis(dataSource,ep_id,Staff_ID,CoursewareID,LearnHistoryMainID,ListID);
		String ReturnXml="";
		Document result=util.getDoc();
		Element root=result.addElement("R");
		root.addElement("Result").addText("1");	
		root.addElement("LearnHistoryMainID").addText(LearnHistoryMainID);	
		root.addElement("ListID").addText(ListID);	
		String W_CoursewareConfig="W_CoursewareConfig_"+ep_id;
		String W_CoursewareFileType="W_CoursewareFileType";
		String sql="select FileTypeDesc,TypeFileKey,CoursewareName,CoursewareDesc,TargetFilePath from "+W_CoursewareConfig+" " +
				"left join "+W_CoursewareFileType+" on "+W_CoursewareFileType+".FileTypeID="+W_CoursewareConfig+".FileType " +
				"where CoursewareID=?";
		Object[] params=new Object[]{CoursewareID};  
		int[] types=new int[]{Types.VARCHAR};  
		List list=util.QueryPersonUseExecute(sql,types,params,dataSource);
		Map map=(Map)list.get(0);
		String CoursewareDesc=(String)map.get("CoursewareDesc");
		String FileTypeDesc=(String)map.get("FileTypeDesc");
		String TypeFileKey=(String)map.get("TypeFileKey");
		String CoursewareName=(String)map.get("CoursewareName");
		String TargetFilePath=(String)map.get("TargetFilePath");
		root.addElement("CoursewareDesc").addText(CoursewareDesc);
		root.addElement("FileTypeDesc").addText(FileTypeDesc);	
		root.addElement("TypeFileKey").addText(TypeFileKey);
		root.addElement("CoursewareName").addText(CoursewareName);	
		root.addElement("TargetFilePath").addText(TargetFilePath);
		ReturnXml=result.asXML();
		log.debug("loadPXInfoServiceImpl_ReturnXml"+ReturnXml);
		return ReturnXml;
		
	}
}
